#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: TournamentsLogic.cs
//

#endregion

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Ids.Utils;
using Ids.Web.Listing;
using Novagoal.Models;
using Novagoal.Models.Utils;
using Novagoal.Repositories;

namespace Novagoal.Logic
{
    public class TournamentsLogic : BaseLogic
    {
        #region CONSTANTS

        

        #endregion

        #region PROPERTIES

        private readonly ITournamentsRepository _tournamentsRepository;

        #endregion

        #region PUBLIC METHODS

        public TournamentsLogic(ITournamentsRepository tournamentsRepository)
        {
            _tournamentsRepository = tournamentsRepository;
        }

        public TournamentList GetTournaments(NameValueCollection filters, string sort, string direction, int currentPage, int pageSize)
        {
            TournamentListSortField sortField;
            SortDirectionType sortDirection;
            Dictionary<TournamentListFilter, string> filterList;

            try
            {
                //Sorting based on parameters
                sortField = sort.ToEnumValue<TournamentListSortField>();
                sortDirection = direction.ToEnumValue<SortDirectionType>();
                filterList = ParseFilters<TournamentListFilter>(filters);
            }
            catch
            {
                //Default Sorting
                filterList = null;
                sortField = TournamentListSortField.Name;
                sortDirection = SortDirectionType.Ascending;
            }

            var items = _tournamentsRepository.GetTournaments(filterList, sortField, sortDirection, currentPage, pageSize);
            var count = _tournamentsRepository.GetTotalTournaments(filterList);

            var teamList = new TournamentList
            {
                CurrentPage = currentPage,
                Filters = filterList,
                Items = items,
                SortDirection = sortDirection,
                SortField = sortField
            };
            teamList.CalculatePagination(count, pageSize, currentPage);

            return teamList;
        }

        public List<Tournament> GetOpenTournaments()
        {
            var tournaments = _tournamentsRepository.GetTournaments();
            return tournaments.Where(t => t.StartAt > DateTime.UtcNow)
                .OrderBy(t => t.Name)
                .ToList();
        }

        public List<Tournament> GetEnabledTournaments()
        {
            var tournaments = _tournamentsRepository.GetTournaments();
            return tournaments.Where(t => t.Enabled)
                .OrderBy(t => t.Name)
                .ToList();
        }

        public Tournament GetTournament(int id)
        {
            return _tournamentsRepository.GetTournament(id);
        }

        #endregion

        #region PRIVATE METHODS

        #endregion
    }
}